/**
 * 
 */
package com.pks.autoInsurance.helper;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import com.pks.autoInsurance.dao.RegistrationDAO;
import com.pks.autoInsurance.dao.RegistrationDAOImpl;
import com.pks.autoInsurance.dao.VehicleDAO;
import com.pks.autoInsurance.dao.VehicleDAOImpl;
import com.pks.autoInsurance.dto.RegistrationDto;
import com.pks.autoInsurance.dto.VehicleDto;

/**
 * Contain user related methods.
 * 
 * @author Pankaj Soni
 * 
 */
public class UserHelper {
	/**
	 * Returns user profile
	 * 
	 * @param ssn
	 * @return RegistrationDto
	 */
	public RegistrationDto getUserProfile(String ssn) {
		System.out.println(getClass() + "::::getUserProfile::::begin");
		System.out.println("::::ssn::::" + ssn);
		RegistrationDAO registrationDAO = new RegistrationDAOImpl();
		return registrationDAO.getUserProfile(ssn);
	}

	/**
	 * Retrives premium quote for a user
	 * 
	 * @param ssn
	 * @return quote for vehicle
	 */
	public Map<String, Object> getQuote(String ssn) {
		System.out.println(getClass() + "::::getQuote::::begin");
		RegistrationDAO registrationDAO = new RegistrationDAOImpl();
		RegistrationDto userInfo = registrationDAO.getUserProfile(ssn);
		VehicleDAO vehicleDAO = new VehicleDAOImpl();
		VehicleDto vehicleDto = vehicleDAO.getVehicle(ssn);
		Map<String, Object> map = new HashMap<String, Object>();
		long timediff = new Date().getTime()
				- userInfo.getDateOfBirth().getTime();
		int convFactor = 1000 * 3600 * 24 * 365;
		float age = (float) timediff / convFactor;
		int totalAccident = vehicleDto.getTotalAccident();
		int policyAmount = vehicleDto.getPolicyAmount();
		float riskFactor = age / 210 + (float) totalAccident / 10;
		float premiumAnnually = policyAmount * riskFactor;
		float premiumQuarterly = premiumAnnually / 4;
		float premiumMonthly = premiumAnnually / 12;
		map.put("userInfo", userInfo);
		map.put("vehicleInfo", vehicleDto);
		map.put("premiumAnnually", String.valueOf((int) premiumAnnually));
		map.put("premiumQuarterly", String.valueOf((int) premiumQuarterly));
		map.put("premiumMonthly", String.valueOf((int) premiumMonthly));
		System.out.println(getClass() + "::::getQuote::::end");
		return map;
	}

}
